package com.kite;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.HashMap;

/**
 * @version V1.0
 * @description:
 * @author: kite
 * @date: 2023/10/26
 */
public class ActivitiBusinessDemo {

    @Test
    public void test1() {
         ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        ProcessInstance evection = runtimeService.startProcessInstanceByKey("evection", "1002");
        System.out.println("流程定义id" + evection.getProcessDefinitionId());
        System.out.println("流程实例id" + evection.getId());
        System.out.println("当前活动id" + evection.getActivityId());
        System.out.println("业务id" + evection.getBusinessKey());
    }

    @Test
    public void test2() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        ProcessDefinition evection = repositoryService.createProcessDefinitionQuery().processDefinitionKey("evection").singleResult();
        boolean suspended = evection.isSuspended();

        if (suspended) {
            repositoryService.activateProcessDefinitionById(evection.getId(), true, null);
            System.out.println("流程定义：" + evection.getName() + ";激活" + "流程定义id:" + evection.getId());
        } else {
            repositoryService.suspendProcessDefinitionById(evection.getId(), true, null);
            System.out.println("流程定义：" + evection.getName() + "; 挂起" + "流程定义id:" + evection.getId());
        }
    }

    @Test
    public void test3() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("17501").singleResult();
        boolean suspended = processInstance.isSuspended();
        if (suspended) {
            runtimeService.activateProcessInstanceById(processInstance.getId());
            System.out.println("流程实例：" + processInstance.getName() + ";激活" + "流程实例id:" + processInstance.getId());
        } else {
            runtimeService.suspendProcessInstanceById(processInstance.getId());
            System.out.println("流程实例：" + processInstance.getName() + "; 挂起" + "流程实例id:" + processInstance.getId());
        }

    }

    @Test
    public void test4() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        Task task = taskService.createTaskQuery().processInstanceId("17501").taskAssignee("zhangsan").singleResult();
        taskService.complete(task.getId());
    }



    @Test
    public void test5() {
       ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        runtimeService.startProcessInstanceByKey("listener","1002");
        System.out.println(defaultProcessEngine.getName());
    }
}
